<?php
/**
 * Created by PhpStorm.
 * User: Happy
 * Date: 2016/6/24 0024
 * Time: 14:46
 */
//上传图片插件
class upload_imagePlugin extends Plugin{

   //上传单个图片示例
  public  function  show_demo(){
       $str=<<<EOT
    <script src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script>
  <input type="file" name="file"  onchange="readFile(this)">
<script>
function readFile(obj){
    var file = obj.files[0];
    //判断类型是不是图片
    if(!/image\/\w+/.test(file.type)){
        alert("请确保文件为图像类型");
        return false;
    }
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function(e){
       // alert(this.result); //就是base64
        $.post('index.php?plugin=upload_image&method=save_image',{image:this.result},function(data){
            alert(data);
        });
    }
}
</script>
EOT;
  echo $str;
die;
  }

  //上传多个文件的演示,返回上传的数组
  public  function  upload_multi_demo(){
      $plugin_path=$this->c('plugin_path');
      $tpl_path=$plugin_path.'/tpl/muti_upload.tpl';
      $content=file_get_contents($tpl_path);
      echo $content;

  }

    //多文件上传接口
  public  function  upload_multiple(){
      $plugin_path=$this->c('plugin_path');
      $upload_lib=$plugin_path.'/lib/fileupload.php';
     include_once($upload_lib);
      $upload_path=$this->c('upload_path');
      $upload=new FileUpload($upload_path);
      $upload->set_tmp_path($plugin_path.'/tmp');
      $upload->upload();
  }
  //压缩图片
 public  function  resize_image($source,$max_width,$max_height,$save_path=null){
        /*
         *  * 网络资源返回
         * var_dump(getimagesize(@'D:\net\meirong2\mobile\8.PNG'));die;
 * array (size=7)
  0 => int 640
  1 => int 640
  2 => int 2
  3 => string 'width="640" height="640"' (length=24)
  'bits' => int 8
  'channels' => int 3
  'mime' => string 'image/jpeg' (length=10)
本地资源返回
array (size=6)
  0 => int 94
  1 => int 96
  2 => int 3
  3 => string 'width="94" height="96"' (length=22)
  'bits' => int 8
  'mime' => string 'image/png' (length=9)
 * */
        $remote=false;
        $tmp_path='';
        //判断网络文件或者实体文件的存在性
        if(substr($source,0,4)=='http'){
            if(!$this->http_file_exist($source)){
                return false; //文件不存在
            }
            //存储为本地临时图片获取资源后在删除图片即可
            $img_resource=file_get_contents($source);
            $new_source=BASE_PATH.'/cache/';
            mk_dir($new_source);
            $new_source.=md5($source);
            file_put_contents($new_source,$img_resource);
            $tmp_path= $source=$new_source;
            $remote=true;//原始资源是远程图片，以便后期删除临时
        }

        if(file_exists($source)){
            $size_src = getimagesize($source);
            $pic_width = $size_src['0'];
            $pic_height = $size_src['1'];
            $pic_type=$size_src['mime'];
            switch($pic_type){
                case 'image/png':
                    $function = 'imagepng';
                    $source = imagecreatefrompng($source);
                    break;
                case 'image/jpeg':
                    $source = imagecreatefromjpeg($source);
                    $function = 'imagejpeg';
                    break;
                case 'image/gif':
                    $function = 'imagegif';
                    $source = imagecreatefromgif($source);
                    break;
                default:
                    $source = imagecreatefromjpeg($source);
                    $function = 'imagejpeg';
                    break;

            }
            $new_width = $pic_width;
            $new_height = $pic_height;
            if ($max_width) { //只按照宽度缩放  //第二个参数是宽度
                $new_width = $max_width;
                $new_height = $pic_height * ($new_width / $pic_width);
            } else if ($max_height) {//只按照高度缩放 //mode后第二个参数是高度
                $new_height = $max_height;
                $new_width = $pic_width * ($new_height / $pic_height);
            } else { //按照最大的边缩放
                $max =$pic_width/2; //按照一半进行缩放
                //按照最大边自由缩放
                if ($pic_width > $pic_height) {
                    $new_width = $max;
                    $new_height = $pic_height * ($max / $pic_width);
                } else {
                    $new_height = $max;
                    $new_width = $pic_width * ($max / $pic_height);
                }
            }

            if (function_exists("imagecopyresampled"))//function_exists("imagecopyresampled") ,真彩图，速度较慢
            {
                $newim = imagecreatetruecolor($new_width, $new_height);
                imagecopyresampled($newim, $source, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
            } else {
                $newim = imagecreate($new_width, $new_height);
                imagecopyresized($newim, $source, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
            }

            mk_dir(dirname($save_path));
            $function($newim, $save_path);//
            imagedestroy($newim);
            if($remote&&file_exists($tmp_path)){
                unlink($tmp_path);
            }
            return true;
        }
        else{
            return false;
        }
    }

    //保存图片，base 64位数值转化为图片
   public  function  save_image($image,$make_thumb=false){
       // $image= $_POST['image']);
      // $image='';

       $pattern='~data:image/(.+?);base64,~';
       preg_match($pattern,$image,$matches);

       if($matches){
           $base64=explode(',',$image);
           $base64=$base64[1];
           $file_type=$matches[1];

           $file_name=date('Ymd_His_').randomString(3).'.'.$file_type;

           $img = base64_decode($base64);
           $upload_path=$this->c('upload_path');
           $part1=date('Y/n/');
          $save_path= $upload_path.'/'.$part1;//以月份指定文件夹即可
           mk_dir($save_path);
           $save_file=$save_path.$file_name;
           while(file_exists($save_file)){  //为了防止文件覆盖，虽然概率很小
               $save_file=$save_path.date('Ymd_His_').randomString(3).'.'.$file_type;
           }
           file_put_contents($save_file,$img);

           //如果需要生成缩略图
           if($make_thumb){
               $save_thumb_file=$upload_path.'/thumb/'.$part1;
               mk_dir($save_thumb_file);
               $save_file_new=$save_thumb_file.$file_name;
               $this->resize_image($save_file,100,null,$save_file_new);
           }
         //  file_put_contents($file_name,$img);
         //  echo $part1.$file_name;
           return  $part1.$file_name;
       }
       else{
           return false;
       }
   }





}